<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box{
            position: relative;
            top:20px;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: indianred;
        }
    </style>
</head>
<body>
    <button id="start">开始</button>
    <button id="end">结束</button>
    <div id="box"></div>
    <script>
        var box = document.getElementById("box");
        var start = document.getElementById("start");
        var end = document.getElementById("end");

        var nowLeft = box.offsetLeft;
        var timer;
        start.onclick = function(){
            clearInterval(timer)
            timer = setInterval(function(){
                nowLeft += 50;
                box.style.left = nowLeft + "px";
            },50);
        }
        end.onclick = function(){
            clearInterval(timer);
        }
    </script>
</body>
</html>